from bluesky.model.resource_m import Resource
from bluesky.service.inner import resource as reosurce_service_module
from bluesky.util import biz

handler_map = {}


class ResourceHandler:

    def key(self):
        return ''

    def before_tx(self, ctx) -> Resource:
        return ctx.resource

    def before_handle(self, ctx) -> Resource:
        return ctx.resource

    def after_handle(self, ctx) -> dict:
        return ctx.result

    def after_tx(self, ctx) -> dict:
        return ctx.result


class ResourceHandlerCtx:
    def __init__(self, action, param, resource, result, request_ctx):
        self.action = action
        self.param = param
        self.resource = resource
        self.result = result
        self.ext = {}
        self.session = request_ctx.get('session')
        self.request_ctx = request_ctx


def init_ctx(action, param, resource, request_ctx):
    return ResourceHandlerCtx(action, param, resource, None, request_ctx)


def init_handler_map():
    biz.import_all_files(reosurce_service_module.__name__)
    clzs = ResourceHandler.__subclasses__()

    for Clz in clzs:
        i = Clz()
        handler_map[i.key()] = i
